﻿using CBS.APP;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// AddClassPlan.xaml 的交互逻辑
    /// </summary>
    public partial class AddClassPlan : Window
    {
        public AddClassPlan()
        {
            InitializeComponent();
            BindSemester();
            BindTeacher();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ClassViewModel classPlan = new();
            classPlan.ClassName = tb_ClassPlan.Text.Trim();
            //classPlan.CoursePrice = string.IsNullOrWhiteSpace(tb_Price.Text.Trim()) ? 0 : decimal.Parse(tb_Price.Text.Trim());
            //classPlan.ClassTimeLong = string.IsNullOrWhiteSpace(tb_Long.Text.Trim()) ? 0 : int.Parse(tb_Long.Text.Trim());
            //classPlan.TeacherId = int.Parse(cb_Teacher.SelectedValue.ToString());
            //classPlan.SemesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
            //classPlan.Memo = tb_Memo.Text.Trim();
            //classPlan.CourseCount = string.IsNullOrWhiteSpace(tb_CourseCount.Text.Trim()) ? 0 : int.Parse(tb_CourseCount.Text.Trim());
            //classPlan.PlanNum = string.IsNullOrWhiteSpace(tb_PlanNum.Text.Trim()) ? 0 : int.Parse(tb_PlanNum.Text.Trim());
            try
            {
                if (ClassApp.CreateClass(classPlan))
                {
                    this.DialogResult = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindSemester()
        {
            var list = await Task.Run(() => SemesterApp.GetSelectList());

            if (list.Count == 0)
            {
                return;
            }

            this.Dispatcher.Invoke(new Action(() =>
            {
                cb_Semester.ItemsSource = list;
            }));
            cb_Semester.SelectedIndex = 0;
            //BindClassPlan();
        }

        private async void BindTeacher()
        {
            var list = await Task.Run(() => TeacherApp.GetSelectList());

            if (list.Count == 0)
            {
                return;
            }

            this.Dispatcher.Invoke(new Action(() =>
            {
                cb_Teacher.ItemsSource = list;
            }));
            cb_Teacher.SelectedIndex = 0;
            //BindClassPlan();
        }
    }
}
